export const stm32f103c8t6_pin = function () {
    return '';
}
export const JMV8_SETUP = function (_, generator) {
    generator.definitions_['0a0'] = 'void attachInterrupt_fun_FALLING_PA15() {\n'+
        ' digitalWrite(PB9,LOW);\n'+
        '}';
    generator.setups_["0a"] = 'pinMode(PB9, OUTPUT);\n'+
        '  pinMode(PA11, INPUT_PULLUP);\n'+
        '  pinmode(PA12, INPUT_PULLUP);\n'+
    '  digitalWrite(PB9, HIGH);\n'+
    '  pinMode(PA15, INPUT_PULLUP);\n'+
    '  while (!digitalRead(PA15)) {}';

    return 'attachInterrupt(digitalPinToInterrupt(PA15),attachInterrupt_fun_FALLING_PA15,FALLING);\n';
}


export const arduino_nrf24l01_send = function (_, generator) {
    var value_nrf24l01_send_data = generator.valueToCode(this, 'nrf24l01_send_data', generator.ORDER_ATOMIC);
    var value_nrf24l01_send_rec_id = generator.valueToCode(this, 'nrf24l01_send_rec_id', generator.ORDER_ATOMIC);

    //generator.setups_['setup_nrf24l01_start_7'] = 'byte data[Mirf.payload];';

    var code = 'byte data[Mirf.payload];' +
        '\ndata[0] = ' + value_nrf24l01_send_data + ' & 0xFF;' +
        '\ndata[1] = ' + value_nrf24l01_send_data + ' >> 8;' +
        '\n\nMirf.setTADDR((byte *)' + value_nrf24l01_send_rec_id + ');' +
        '\nMirf.send(data);\n';
    return code;
};
export const arduino_nrf24l01_is_sending = function (_, generator) {

    var code = 'Mirf.isSending()';

    return [code, generator.ORDER_ATOMIC];
};
export const arduino_nrf24l01_data_ready = function (_, generator) {

    var code = 'Mirf.dataReady()';

    return [code, generator.ORDER_ATOMIC];
};
export const arduino_nrf24l01_get_data = function () {

    var code = 'Mirf.getData(data);\n';
    return code;
};

export const make_arduino_nrf24l01_begin = function (_, generator) {
    var value_nrf24l01_ce = generator.valueToCode(this, 'nrf24l01_ce', generator.ORDER_ATOMIC);
    var value_nrf24l01_csn = generator.valueToCode(this, 'nrf24l01_csn', generator.ORDER_ATOMIC);
    var statements_nrf24l01_setting = generator.statementToCode(this, 'nrf24l01_setting');

    generator.definitions_['include_SPI'] = '#include <SPI.h>';
    generator.definitions_['include_Mirf'] = '#include <Mirf.h>';
    generator.definitions_['include_nRF24L01'] = '#include <nRF24L01.h>';
    generator.definitions_['include_MirfHardwareSpiDriver'] = '#include <MirfHardwareSpiDriver.h>';

    generator.setups_['setup_nrf24l01_begin'] = 'Mirf.cePin = ' + value_nrf24l01_ce + ';'
        + '\n  Mirf.csnPin = ' + value_nrf24l01_csn + ';'
        + '\n  Mirf.spi = &MirfHardwareSpi;'
        + '\n  Mirf.init();'
        + '\n' + statements_nrf24l01_setting + '  Mirf.config();';
    var code = '';
    return code;
};

export const make_arduino_nrf24l01_setRADDR = function (_, generator) {
    var value_nrf24l01_set_data = generator.valueToCode(this, 'nrf24l01_set_data', generator.ORDER_ATOMIC);
    var code = 'Mirf.setRADDR((byte *)' + value_nrf24l01_set_data + ');\n';
    return code;
};

export const make_arduino_nrf24l01_payload = function (_, generator) {
    var value_nrf24l01_set_data = generator.valueToCode(this, 'nrf24l01_set_data', generator.ORDER_ATOMIC);
    var code = 'Mirf.payload = ' + value_nrf24l01_set_data + ';\n';
    return code;
};

export const make_arduino_nrf24l01_payload_return = function (_, generator) {
    var code = 'Mirf.payload';
    return [code, generator.ORDER_ATOMIC];
};

export const make_arduino_nrf24l01_channel = function (_, generator) {
    var value_nrf24l01_set_data = generator.valueToCode(this, 'nrf24l01_set_data', generator.ORDER_ATOMIC);
    var code = 'Mirf.channel = ' + value_nrf24l01_set_data + ';\n';
    return code;
};

export const make_arduino_nrf24l01_sendData = function (_, generator) {
    var value_nrf24l01_send_data = generator.valueToCode(this, 'nrf24l01_send_data', generator.ORDER_ATOMIC);
    var value_nrf24l01_settaddr = generator.valueToCode(this, 'nrf24l01_settaddr', generator.ORDER_ATOMIC);
    var code = 'Mirf.setTADDR((byte *)' + value_nrf24l01_settaddr + ');'
        + '\nMirf.send(' + value_nrf24l01_send_data + ');\n';
    return code;
};

export const make_arduino_nrf24l01_isSending = function (_, generator) {
    var code = 'Mirf.isSending()';
    return [code, generator.ORDER_ATOMIC];
};

export const make_arduino_nrf24l01_dataReady = function (_, generator) {
    var code = 'Mirf.dataReady()';
    return [code, generator.ORDER_ATOMIC];
};

export const make_arduino_nrf24l01_getData = function (_, generator) {
    var value_nrf24l01_get_data = generator.valueToCode(this, 'nrf24l01_get_data', generator.ORDER_ATOMIC);
    var code = 'Mirf.getData(' + value_nrf24l01_get_data + ');\n';
    return code;
};

//初始化nRF24L01无线通信模块
export const make_arduino_rf24_begin = function (_, generator) {
    var text_name = this.getFieldValue('name');

    generator.definitions_['include_SPI'] = '#include <SPI.h>';
    generator.definitions_['include_nRF24L01'] = '#include "nRF24L01_STM32.h"';
    generator.definitions_['include_RF24'] = '#include "RF24_STM32.h"';
    generator.definitions_['var_declare_spi'] = 'SPIClass SPI_2(PB15, PB14, PB13, PB12);';

    generator.definitions_['var_declare_rf24' + text_name] = 'RF24_STM32 ' + text_name + '(PC9, PC6, &SPI_2);';
    generator.setups_['0setup_spi'] = 'SPI_2.begin();';
    generator.setups_['setup_rf24_' + text_name] = text_name + '.begin();';
    var code = '';
    return code;
};

//nRF24L01无线通信模块 获取数据
export const make_arduino_rf24_get_data = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var dropdown_type = this.getFieldValue('type');
    var code = '';
    if (dropdown_type == 'failureDetected') {
        code = text_name + '.' + dropdown_type;
    }
    else {
        code = text_name + '.' + dropdown_type + '()';
    }
    return [code, generator.ORDER_ATOMIC];
};

//nRF24L01无线通信模块 执行某些函数
export const make_arduino_rf24_do = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var dropdown_type = this.getFieldValue('type');
    if (dropdown_type == 'printDetails') {
        generator.definitions_['include_printf'] = '#include "printf.h"';
        generator.setups_['setup_printf_begin'] = 'printf_begin();';
        var code = text_name + '.' + dropdown_type + '();\n';
    }
    else {
        var code = text_name + '.' + dropdown_type + '();\n';
    }

    return code;
};

//nRF24L01无线通信模块 打开发送管道
export const make_arduino_rf24_openWritingPipe = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_address = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);
    var code = text_name + '.openWritingPipe(' + value_address + ');\n';
    return code;
};

//nRF24L01无线通信模块 打开接收管道
export const make_arduino_rf24_openReadingPipe = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_number = generator.valueToCode(this, 'number', generator.ORDER_ATOMIC);
    var value_address = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);
    var code = text_name + '.openReadingPipe(' + value_number + ', ' + value_address + ');\n';
    return code;
};

//nRF24L01无线通信模块 接收管道编号
export const make_arduino_rf24_pipe_type = function (_, generator) {
    var dropdown_type = this.getFieldValue('type');
    var code = dropdown_type;
    return [code, generator.ORDER_ATOMIC];
};

//nRF24L01无线通信模块 关闭接收管道
export const make_arduino_rf24_closeReadingPipe = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_number = generator.valueToCode(this, 'number', generator.ORDER_ATOMIC);
    var code = text_name + '.closeReadingPipe(' + value_number + ');\n';
    return code;
};

//nRF24L01无线通信模块 接收到数据？ 保存接收管道编号到变量
export const make_arduino_rf24_available = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_pipe_num = generator.valueToCode(this, 'pipe_num', generator.ORDER_ATOMIC);
    var code = text_name + '.available(&' + value_pipe_num + ')';
    return [code, generator.ORDER_ATOMIC];
};

//nRF24L01无线通信模块 设置地址宽度
export const make_arduino_rf24_setAddressWidth = function (_, generator) {
    var value_width = generator.valueToCode(this, 'width', generator.ORDER_ATOMIC);
    var text_name = this.getFieldValue('name');
    var code = text_name + '.setAddressWidth(' + value_width + ');\n';
    return code;
};

//nRF24L01无线通信模块 定义可用的地址宽度
export const make_arduino_rf24_AddressWidth_data = function (_, generator) {
    var dropdown_type = this.getFieldValue('type');
    var code = dropdown_type;
    return [code, generator.ORDER_ATOMIC];
};

//nRF24L01无线通信模块 设置射频通道
export const make_arduino_rf24_setChannel = function (_, generator) {
    var value_channel = generator.valueToCode(this, 'channel', generator.ORDER_ATOMIC);
    var text_name = this.getFieldValue('name');
    var code = text_name + '.setChannel(' + value_channel + ');\n';
    return code;
};

//nRF24L01无线通信模块 设置射频功率
export const make_arduino_rf24_setPALevel = function () {
    var text_name = this.getFieldValue('name');
    var dropdown_type = this.getFieldValue('type');
    var code = text_name + '.setPALevel(' + dropdown_type + ');\n';
    return code;
};

//nRF24L01无线通信模块 设置空中波特率
export const make_arduino_rf24_setDataRate = function () {
    var text_name = this.getFieldValue('name');
    var dropdown_type = this.getFieldValue('type');
    var code = text_name + '.setDataRate(' + dropdown_type + ');\n';
    return code;
};

//nRF24L01无线通信模块 设置CRC校验的校验长度
export const make_arduino_rf24_setCRCLength = function (_, generator) {
    var value_length = generator.valueToCode(this, 'length', generator.ORDER_ATOMIC);
    var text_name = this.getFieldValue('name');
    var code = text_name + '.setCRCLength(' + value_length + ');\n';
    return code;
};

//nRF24L01无线通信模块 可设置的CRC校验的校验长度类型
export const make_arduino_rf24_setCRCLength_data = function (_, generator) {
    var dropdown_type = this.getFieldValue('type');
    var code = dropdown_type;
    return [code, generator.ORDER_ATOMIC];
};

//nRF24L01无线通信模块 设置静态载荷的长度
export const make_arduino_rf24_setPayloadSize = function (_, generator) {
    var value_length = generator.valueToCode(this, 'length', generator.ORDER_ATOMIC);
    var text_name = this.getFieldValue('name');
    var code = text_name + '.setPayloadSize(' + value_length + ');\n';
    return code;
};

//nRF24L01无线通信模块 启用或禁用自动应答
export const make_arduino_rf24_setAutoAck_all = function (_, generator) {
    var value_enable = generator.valueToCode(this, 'enable', generator.ORDER_ATOMIC);
    var text_name = this.getFieldValue('name');
    var code = text_name + '.setAutoAck(' + value_enable + ');\n';
    return code;
};

//开启 - 关闭
export const make_arduino_open_close = function (_, generator) {
    var dropdown_type = this.getFieldValue('type');
    var code = dropdown_type;
    return [code, generator.ORDER_ATOMIC];
};

//nRF24L01无线通信模块 在某个管道上启用或禁用自动应答
export const make_arduino_rf24_setAutoAck = function (_, generator) {
    var value_pipe = generator.valueToCode(this, 'pipe', generator.ORDER_ATOMIC);
    var text_name = this.getFieldValue('name');
    var value_enable = generator.valueToCode(this, 'enable', generator.ORDER_ATOMIC);
    var code = text_name + '.setAutoAck(' + value_pipe + ', ' + value_enable + ');\n';
    return code;
};

//nRF24L01无线通信模块 设置自动重发延时和发送次数
export const make_arduino_rf24_setRetries = function (_, generator) {
    var value_delay = generator.valueToCode(this, 'delay', generator.ORDER_ATOMIC);
    var text_name = this.getFieldValue('name');
    var value_count = generator.valueToCode(this, 'count', generator.ORDER_ATOMIC);
    var code = text_name + '.setRetries(' + value_delay + ', ' + value_count + ');\n';
    return code;
};

//nRF24L01无线通信模块 屏蔽中断
export const make_arduino_rf24_maskIRQ = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_tx_ok = generator.valueToCode(this, 'tx_ok', generator.ORDER_ATOMIC);
    var value_tx_fail = generator.valueToCode(this, 'tx_fail', generator.ORDER_ATOMIC);
    var value_rx_ready = generator.valueToCode(this, 'rx_ready', generator.ORDER_ATOMIC);
    var code = text_name + '.maskIRQ(' + value_tx_ok + ', ' + value_tx_fail + ', ' + value_rx_ready + ');\n';
    return code;
};

//nRF24L01无线通信模块 获取中断
export const make_arduino_rf24_whatHappened = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_tx_ok = generator.valueToCode(this, 'tx_ok', generator.ORDER_ATOMIC);
    var value_tx_fail = generator.valueToCode(this, 'tx_fail', generator.ORDER_ATOMIC);
    var value_rx_ready = generator.valueToCode(this, 'rx_ready', generator.ORDER_ATOMIC);
    var code = text_name + '.whatHappened(' + value_tx_ok + ', ' + value_tx_fail + ', ' + value_rx_ready + ');\n';
    return code;
};

//nRF24L01无线通信模块 发送数据
export const make_arduino_rf24_write = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_buf = generator.valueToCode(this, 'buf', generator.ORDER_ATOMIC);
    var dropdown_type = this.getFieldValue('type');
    var value_len = generator.valueToCode(this, 'len', generator.ORDER_ATOMIC);
    var value_multicast = generator.valueToCode(this, 'multicast', generator.ORDER_ATOMIC);
    var code = text_name + '.' + dropdown_type + '(&' + value_buf + ', ' + value_len + ', ' + value_multicast + ');\n';
    return code;
};

//nRF24L01无线通信模块 发送数据，返回发送结果
export const make_arduino_rf24_write_return = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_buf = generator.valueToCode(this, 'buf', generator.ORDER_ATOMIC);
    var dropdown_type = this.getFieldValue('type');
    var value_len = generator.valueToCode(this, 'len', generator.ORDER_ATOMIC);
    var value_multicast = generator.valueToCode(this, 'multicast', generator.ORDER_ATOMIC);
    var code = text_name + '.' + dropdown_type + '(&' + value_buf + ', ' + value_len + ', ' + value_multicast + ')';
    return [code, generator.ORDER_ATOMIC];
};

//nRF24L01无线通信模块 发送数据(可设置超时时间)
export const make_arduino_rf24_writeBlocking = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_buf = generator.valueToCode(this, 'buf', generator.ORDER_ATOMIC);
    var value_len = generator.valueToCode(this, 'len', generator.ORDER_ATOMIC);
    var value_timeout = generator.valueToCode(this, 'timeout', generator.ORDER_ATOMIC);
    var code = text_name + '.writeBlocking(&' + value_buf + ', ' + value_len + ', ' + value_timeout + ');\n';
    return code;
};

//nRF24L01无线通信模块 发送数据(可设置超时时间)，返回发送结果
export const make_arduino_rf24_writeBlocking_return = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_buf = generator.valueToCode(this, 'buf', generator.ORDER_ATOMIC);
    var value_len = generator.valueToCode(this, 'len', generator.ORDER_ATOMIC);
    var value_timeout = generator.valueToCode(this, 'timeout', generator.ORDER_ATOMIC);
    var code = text_name + '.writeBlocking(&' + value_buf + ', ' + value_len + ', ' + value_timeout + ')';
    return [code, generator.ORDER_ATOMIC];
};

//nRF24L01无线通信模块 写入ACK数据包
export const make_arduino_rf24_writeAckPayload = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_pipe = generator.valueToCode(this, 'pipe', generator.ORDER_ATOMIC);
    var value_buf = generator.valueToCode(this, 'buf', generator.ORDER_ATOMIC);
    var value_len = generator.valueToCode(this, 'len', generator.ORDER_ATOMIC);
    var code = text_name + '.writeAckPayload(' + value_pipe + ', &' + value_buf + ', ' + value_len + ');\n';
    return code;
};

//nRF24L01无线通信模块 读取数据
export const make_arduino_rf24_read = function (_, generator) {
    var text_name = this.getFieldValue('name');
    var value_buf = generator.valueToCode(this, 'buf', generator.ORDER_ATOMIC);
    var value_len = generator.valueToCode(this, 'len', generator.ORDER_ATOMIC);
    var code = text_name + '.read(&' + value_buf + ', ' + value_len + ');\n';
    return code;
};

//nRF24L01无线通信模块 数据长度定义
export const make_arduino_rf24_length = function (_, generator) {
    var dropdown_type = this.getFieldValue('type');
    var code = dropdown_type;
    return [code, generator.ORDER_ATOMIC];
};

//nRF24L01无线通信模块 射频通道定义
export const make_arduino_rf24_channel = function (_, generator) {
    var dropdown_type = this.getFieldValue('type');
    var code = dropdown_type;
    return [code, generator.ORDER_ATOMIC];
};

//nRF24L01无线通信模块 重发次数定义
export const make_arduino_rf24_count = function (_, generator) {
    var dropdown_type = this.getFieldValue('type');
    var code = dropdown_type;
    return [code, generator.ORDER_ATOMIC];
};

export const make_tool_normal = function (_, generator) {
    var value_data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    var code = value_data + ';\n';
    return code;
};